<template>
    <div>
          <span>姓名：</span><el-input v-model="username" placeholder="请输入姓名" style="width: 40%;"></el-input><el-button type="primary" style="margin-left:8px" @click="query">查询</el-button><el-button type="primary" style="margin-left:8px" @click="addUserInfo">新增</el-button>
        <el-table
      :data="tableData"
      height="600"
      style="width: 100%">
      <el-table-column
        prop="username"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="mail"
        label="邮箱"
        width="180">
      </el-table-column>
      <el-table-column
        prop="phone"
        label="手机号">
      </el-table-column>
      <el-table-column
        prop="age"
        label="年龄">
      </el-table-column>
      <el-table-column
      fixed="right"
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-button @click="showEdit(scope.row)" type="text" size="small">编辑</el-button>
       <el-popconfirm
          title="是否要删除这条记录？" @confirm="del(scope.row)"
       >
          <el-button slot="reference" type="text">删除</el-button>
       </el-popconfirm>
      </template>
    </el-table-column>
    </el-table>
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage2"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="sizes, prev, pager, next"
      :total="total">
    </el-pagination>
    <el-dialog
        :title="titleName"
        :visible.sync="dialogVisible"
        width="50%"
        :before-close="handleClose">
        <el-form ref="form" :model="form" label-width="80px" :rules="rules">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="mail">
              <el-input v-model="form.mail"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone"></el-input>
            </el-form-item>
            <el-form-item label="年龄" prop="age">
              <el-input v-model="form.age"></el-input>
        </el-form-item>
     </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="insertUser('form')">确 定</el-button>
        </span>
    </el-dialog>
    </div>
</template>
<script>
import {getUserAll,getUserByName,addUser,updateUser,delUser,fenye} from '../api/data.js'
export default{
    name:"page2",
    data() {
        return {
          total:0,
          pageNum:1,
          pageSize:10,   
          tableData: [],
          username:'',
          dialogVisible: false,
          form: {
              username: '',
              delivery: false,
              form:{
                username:'',
                phone:'',
                mail:'',
                age:'' 
              }
         },
         titleName:'',
         visible: false,
         rules: {
            username: [
                { required: true, message: '请输入用户名称', trigger: 'blur' },
                { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
              ],
            mail:[{ required: true, message: '请输入邮箱', trigger: 'blur' }],
            phone:[{ required: true, message: '请输入电话号码', trigger: 'blur' }],
            age:[{ required: true, message: '请输入年龄', trigger: 'blur' },                
            { min: 1, max: 3, message: '长度在 1 到 3 个字符', trigger: 'blur' }
]
         }
        }
      },
      mounted(){
        getUserAll().then(res=>{
          console.log("获取全部用户",res)
        this.total = res.data.length
        })  
        this.queryFenye()
      },
      methods:{
          queryFenye(){
            fenye({pageNum:this.pageNum,pageSize:this.pageSize}).then(res=>{
              this.tableData = res.data
            })
          },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize=val
        this.queryFenye()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.pageNum = val
        this.queryFenye()
      },
        query(){
          if(this.username==""){
            getUserAll().then(res=>{
              console.log("获取全部用户",res)
              this.tableData = res.data
            })  
          }
          else{
            getUserByName({username:this.username}).then(res=>{
            console.log("通过用户名获取",res)
            this.tableData=[]
            this.tableData.push(res.data)
          })
          }
        },
        addUserInfo(){
            this.dialogVisible = true
            this.titleName='新增'
        },
        handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      insertUser(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.titleName=="新增"){
                addUser(this.form).then(res=>{
                console.log("新增用户",res)
                if(res.data=="新增成功",res){
                  this.$message({
                    message:'新增成功',
                    type:'succes'
                  });
                  getUserAll().then(res=>{
                    console.log("获取全部用户",res)
                    this.tableData = res.data
                })
              }
              else{
                this.message.console.error("新增失败");
              }
              this.dialogVisible=false
            })
          }
          else{
            updateUser(this.form).then(res=>{
              console.log("更新用户")
              if(res.data=="更新成功",res){
                  this.$message.success("更新成功")
                  getUserAll().then(res=>{
                    console.log("获取全部用户",res)
                    this.tableData = res.data
                })
              }
              else{
                this.message.console.error("编辑失败");
              }
              this.dialogVisible=false
            })
            
          }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
        
    },
      showEdit(row){
        console.log("当前用户",row)
        this.dialogVisible=true
        this.titleName='编辑'
        this.form.username=row.username
        this.form.mail=row.mail
        this.form.phone=row.phone
        this.form.age=row.age
        this.form.id=row.id
      },
      del(row){
        console.log("删除信息",row)
        delUser({id:row.id}).then(res=>{
          if(res.data=="删除成功"){
             this.$message.success("删除成功")
            getUserAll().then(res=>{
              console.log("获取全部用户",res)
              this.tableData = res.data
          })
          }
          else{
            this.message.console.error("删除失败");
          }
        })
      }
    }
  }

</script>

<style>

</style>